/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.unindra.raport.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import edu.unindra.raport.dao.entity.Operator;
import edu.unindra.raport.dao.interf.OperatorInterf;
import edu.unindra.raport.dao.koneksi.DatabaseUtilities;

/**
 * 
 * @author fendi
 */
public class OperatorImpl implements OperatorInterf {

	@Override
	public int insert(Operator op) throws SQLException {
		int result = -1;
		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities
					.getInstance()
					.getConnection()
					.prepareStatement("insert into operator values (?,?,?,?,?)");

			ps.setString(1, op.getId());
			ps.setString(2, op.getPassword());
			ps.setString(3, op.getName());
			ps.setString(4, op.getAddress());
			ps.setString(5, op.getContact());

			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}

		return result;
	}

	@Override
	public Operator getOperator() throws SQLException {
		Operator op = new Operator();

		Statement st = null;
		ResultSet rs = null;
		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("select * from operator");
			while (rs.next()) {
				op.setId(rs.getString("ID_OPERATOR"));
				op.setPassword(rs.getString("PASSWORD_OPERATOR"));
				op.setName(rs.getString("NAMA_OPERATOR"));
				op.setAddress(rs.getString("ALAMAT_OPERATOR"));
				op.setContact(rs.getString("KONTAK_OPERATOR"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return op;
	}

	public boolean login(String id, String pass) throws SQLException {
		List<Operator> list = new LinkedList<Operator>();
		Operator op = new Operator();

		Statement st = null;
		ResultSet rs = null;
		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("select * from operator where ID_OPERATOR = '"
					+ id + "' and PASSWORD_OPERATOR ='" + pass + "'");
			while (rs.next()) {
				op.setId(rs.getString("ID_OPERATOR"));
				op.setPassword(rs.getString("PASSWORD_OPERATOR"));
				op.setName(rs.getString("NAMA_OPERATOR"));
				op.setAddress(rs.getString("ALAMAT_OPERATOR"));
				op.setContact(rs.getString("KONTAK_OPERATOR"));

				list.add(op);
			}

			if (list.size() > 0) {
				return true;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return false;
	}

	@Override
	public List<Operator> getOperatorAll() throws SQLException {
		List<Operator> opr = new LinkedList<Operator>();
		Operator op = null;

		Statement st = null;
		ResultSet rs = null;
		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("select * from operator");
			while (rs.next()) {
				op = new Operator();

				op.setId(rs.getString("ID_OPERATOR"));
				op.setPassword(rs.getString("PASSWORD_OPERATOR"));
				op.setName(rs.getString("NAMA_OPERATOR"));
				op.setAddress(rs.getString("ALAMAT_OPERATOR"));
				op.setContact(rs.getString("KONTAK_OPERATOR"));

				opr.add(op);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return opr;
	}

	@Override
	public int delete(String id) throws SQLException {
		int result = -1;
		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities
					.getInstance()
					.getConnection()
					.prepareStatement(
							"delete from operator where ID_OPERATOR = ?");

			ps.setString(1, id);
			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}

		return result;
	}

	@Override
	public int update(Operator op) throws SQLException {
		int result = -1;

		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities
					.getInstance()
					.getConnection()
					.prepareStatement(
							"update operator set PASSWORD_OPERATOR = ?,NAMA_OPERATOR = ?,ALAMAT_OPERATOR = ?,KONTAK_OPERATOR = ? where ID_OPERATOR = ? ");

			ps.setString(1, op.getPassword());
			ps.setString(2, op.getName());
			ps.setString(3, op.getAddress());
			ps.setString(4, op.getContact());
			ps.setString(5, op.getId());

			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}
		return result;
	}

}
